import java.util.*;


public class code2{
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 判断岛屿数量
     * @param grid char字符型二维数组 
     * @return int整型
     */
    int m;
    int n;
    public int solve (char[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int ret = 0;

        for(int i=0;i<m;i++) {
            for(int j=0;j<n;j++) {
                if(grid[i][j] == '1') {
                    ret++;
                    dfs(grid,i,j);
                }
            }
        }
        return ret;
    }

    private void dfs(char[][] grid,int i,int j) {
        //越界
        if(i<0 || i>=m || j<0 || j>=n) {
            return;
        }

        if(grid[i][j] == '0') return;
        if (grid[i][j] == '1') grid[i][j] = '0';

        dfs(grid,i+1,j);
        dfs(grid,i-1,j);
        dfs(grid,i,j-1);
        dfs(grid,i,j+1);

    }
}